Factory Pattern এবং Strategy Pattern

Computer Programming - এপেক্স (Apex) Apex Design Patterns (Apex Design Patterns) |
276
276

Factory Pattern এবং Strategy Pattern হলো দুইটি গুরুত্বপূর্ণ ডিজাইন প্যাটার্ন, যা Apex (Salesforce)-এ কোডের স্থায়িত্ব, রিইউজেবলিটি, এবং মেইনটেনেবিলিটি বৃদ্ধিতে সহায়ক। নিচে এই প্যাটার্নগুলো নিয়ে আলোচনা করা হলো:


Factory Pattern

Factory Pattern হল একটি Creational Design Pattern, যা ক্লায়েন্টের সাথে সরাসরি কোনো নির্দিষ্ট ক্লাসের উপর নির্ভরশীলতা কমায়। এর মাধ্যমে আমরা একটি Factory Class তৈরি করি, যা নির্দিষ্ট ক্রাইটেরিয়ার ভিত্তিতে বিভিন্ন ধরনের অবজেক্ট তৈরি করে।

Key Concepts:

  • Factory Class: অবজেক্ট তৈরির দায়িত্ব থাকে এই ক্লাসের উপর।
  • Abstract Class/Interface: একটি ভিত্তি ক্লাস বা ইন্টারফেস যার বিভিন্ন ইমপ্লিমেন্টেশন Factory-এর মাধ্যমে তৈরি হবে।

উদাহরণ (Apex):

ধরুন, আমরা বিভিন্ন ধরনের Notification (যেমন Email, SMS) তৈরি করবো:

public interface Notification {
    void send(String message);
}

public class EmailNotification implements Notification {
    public void send(String message) {
        System.debug('Sending Email: ' + message);
    }
}

public class SMSNotification implements Notification {
    public void send(String message) {
        System.debug('Sending SMS: ' + message);
    }
}

public class NotificationFactory {
    public static Notification createNotification(String type) {
        if (type == 'EMAIL') {
            return new EmailNotification();
        } else if (type == 'SMS') {
            return new SMSNotification();
        }
        return null;
    }
}

// Usage
Notification notification = NotificationFactory.createNotification('EMAIL');
notification.send('Hello!');

Strategy Pattern

Strategy Pattern হলো একটি Behavioral Design Pattern, যা একই কাজের বিভিন্ন অ্যালগরিদমের জন্য ভিন্ন ভিন্ন স্ট্র্যাটেজি (অ্যালগরিদম) আলাদা করে রাখে। এর ফলে আমরা রানটাইমে অ্যালগরিদমগুলো পরিবর্তন করতে পারি।

Key Concepts:

  • Strategy Interface: যা বিভিন্ন স্ট্র্যাটেজি (অ্যালগরিদম) ইমপ্লিমেন্ট করতে হবে।
  • Concrete Strategy Classes: বিভিন্ন স্ট্র্যাটেজি ক্লাস, যেগুলো নির্দিষ্ট অ্যালগরিদম অনুসারে কাজ করবে।
  • Context Class: ক্লায়েন্টের জন্য কাজটি সম্পন্ন করতে স্ট্র্যাটেজি অবজেক্ট ব্যবহার করে।

উদাহরণ (Apex):

ধরুন, আমাদের কাছে বিভিন্ন পেমেন্ট মেথড রয়েছে, যেমন CreditCard ও PayPal।

public interface PaymentStrategy {
    void pay(Decimal amount);
}

public class CreditCardPayment implements PaymentStrategy {
    public void pay(Decimal amount) {
        System.debug('Paid ' + amount + ' using Credit Card');
    }
}

public class PayPalPayment implements PaymentStrategy {
    public void pay(Decimal amount) {
        System.debug('Paid ' + amount + ' using PayPal');
    }
}

public class PaymentContext {
    private PaymentStrategy strategy;
    
    public PaymentContext(PaymentStrategy strategy) {
        this.strategy = strategy;
    }
    
    public void executePayment(Decimal amount) {
        strategy.pay(amount);
    }
}

// Usage
PaymentContext context = new PaymentContext(new CreditCardPayment());
context.executePayment(100.00);

context = new PaymentContext(new PayPalPayment());
context.executePayment(200.00);

সংক্ষেপে তুলনা:

  • Factory Pattern: বিভিন্ন অবজেক্ট তৈরি করে, যেগুলো নির্দিষ্ট ইন্টারফেস ইমপ্লিমেন্ট করে।
  • Strategy Pattern: একই কাজের বিভিন্ন অ্যালগরিদম/স্ট্র্যাটেজি নির্ধারণ করতে সহায়ক, যা রানটাইমে পরিবর্তনযোগ্য।

এই প্যাটার্নগুলো ব্যবহার করে আপনি Apex-এ আপনার কোডের স্থায়িত্ব এবং রিইউজেবলিটি বাড়াতে পারবেন।

common.content_added_by
টপ রেটেড অ্যাপ

স্যাট অ্যাকাডেমী অ্যাপ

আমাদের অল-ইন-ওয়ান মোবাইল অ্যাপের মাধ্যমে সীমাহীন শেখার সুযোগ উপভোগ করুন।

ভিডিও
লাইভ ক্লাস
এক্সাম
ডাউনলোড করুন
Promotion